﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InCom.Utilities.Extension
{
    /// <summary>
    /// 协议解析用的一些扩展方法
    /// </summary>
    public static class ParseProtocolBytesHelper
    {

        /// <summary>
        /// Asc码
        /// </summary>
        static System.Text.Encoding asc2 = System.Text.ASCIIEncoding.ASCII;

        /// <summary>
        ///GBK码
        /// </summary>
        static System.Text.Encoding GBkEncoding = System.Text.Encoding.GetEncoding("GBK");
        /// <summary>
        /// GB2312码
        /// </summary>
        static System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");

        /// <summary>
        ///返回指定长度的解析字符串,不足在后补0x00,超过自动截取指定长度
        /// </summary>
        /// <returns>返回指定长度的解析字符串</returns>
        public static byte[] ParseLenStringAscII(this string val,int len )
        {
            return ParseLenString(val, len, asc2);
        }

        /// <summary>
        ///返回指定长度的解析字符串,不足在后补0x00,超过自动截取指定长度
        /// </summary>
        /// <returns>返回指定长度的解析字符串</returns>
        public static byte[] ParseLenStringGBK(this string val, int len)
        {
            return ParseLenString(val, len, GBkEncoding);
        }
        /// <summary>
        ///返回指定长度的解析字符串,不足在后补0x00,超过自动截取指定长度
        /// </summary>
        /// <returns>返回指定长度的解析字符串</returns>
        public static byte[] ParseLenStringGB2312(this string val, int len)
        {
            return ParseLenString(val, len, GB2312);
        }

        /// <summary>
        /// 返回指定长度的解析字符串,不足在后补0x00,超过自动截取指定长度
        /// </summary>
        /// <param name="len">长度</param>
        /// <param name="val">需要处理的字符串</param>
        /// <param name="enc">编码</param>
        /// <returns></returns>
        public static byte[] ParseLenString(this string val, int len, System.Text.Encoding enc)
        {
             byte[] resultBytes = new byte[len];
            //值有效
            if (!String.IsNullOrEmpty(val))
            {
                byte[] strBytes = enc.GetBytes(val);
                //复制字符串后面补0
                Buffer.BlockCopy(strBytes, 0, resultBytes, 0, Math.Min(resultBytes.Length, strBytes.Length));
            }
            return resultBytes;
        }

        /// <summary>
        /// 返回指定长度的解析字节数组,不足在后补0x00,超过自动截取指定长度
        /// </summary>
        /// <param name="len">长度</param>
        /// <param name="val">需要处理的字节数据</param>
         /// <returns></returns>
        public static byte[] ParseLenBytes(this byte[] val, int len)
        {
            //VIN  长度17
            byte[] resultBytes = new byte[len];
            //车架号有效
            if (val !=null)
            {
                byte[] tmpval = val;
                //复制车架号
                Buffer.BlockCopy(tmpval, 0, resultBytes, 0, Math.Min(resultBytes.Length, tmpval.Length));
            }
            return resultBytes;
        }



    }
}
